<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>学号管理</el-breadcrumb-item>
      <el-breadcrumb-item>学号列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入学号"
            v-model="queryInfo.query"
            clearable
            @clear="getStudentList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
             @click="queryStudent"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true"
            >添加学生</el-button
          >
        </el-col>
      </el-row>

      <!-- 学生列表区域 -->
      <el-table :data="userlist" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="name" label="姓名" width="180">
        </el-table-column>
        <el-table-column prop="sex" label="性别"> </el-table-column>
        <el-table-column prop="id" label="学号"> </el-table-column>
        <el-table-column prop="department" label="学院"> </el-table-column>
        <el-table-column prop="major" label="专业"> </el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteStudent(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加学生的对话框 -->
    <el-dialog
      title="添加学生"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addDialogClosed"
    >
      <!-- 内容主体区域 -->
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="70px"
      >
        <el-form-item prop="name" label="姓名">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item prop="sex" label="性别">
          <el-input v-model="addForm.sex"></el-input>
        </el-form-item>
        <el-form-item prop="id" label="学号">
          <el-input v-model="addForm.id"></el-input>
        </el-form-item>
        <el-form-item prop="department" label="学院">
          <el-input v-model="addForm.department"></el-input>
        </el-form-item>
        <el-form-item prop="major" label="专业">
          <el-input v-model="addForm.major"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addStudent">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改学生对话框 -->
     <el-dialog
      title="修改学生"
      :visible.sync="editDialogVisible"
      width="50%"
      @click="editDialogClosed"
    >
      <!-- 内容主体区域 -->
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="70px"
      >
        <el-form-item prop="name" label="姓名">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
        <el-form-item prop="sex" label="性别">
          <el-input v-model="editForm.sex"></el-input>
        </el-form-item>
        <el-form-item prop="id" label="学号">
          <el-input v-model="editForm.id" disabled></el-input>
        </el-form-item>
        <el-form-item prop="department" label="学院">
          <el-input v-model="editForm.department"></el-input>
        </el-form-item>
        <el-form-item prop="major" label="专业">
          <el-input v-model="editForm.major"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editStudent">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 获取学生列表的参数对象
      queryInfo: {
        query: ''
      },
      userlist: [],
      total: 0,
      // 控制添加学生对话框的显示与隐藏
      addDialogVisible: false,
      // 控制修改学生对话框的显示与隐藏
      editDialogVisible: false,
      // 添加学生的表单数据
      addForm: {
        name: '',
        sex: '',
        id: '',
        department: '',
        major: ''
      },
      editForm: {},
      // 添加表单的验证规则对象
      addFormRules: {
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 1,
            max: 20,
            message: '用户名的长度在3~20个字符之间',
            trigger: 'blur'
          }
        ],
        sex: [
          { required: true, message: '请输入性别', trigger: 'blur' },
          {
            min: 1,
            max: 5,
            message: '用户名的长度在1~5个字符之间',
            trigger: 'blur'
          }
        ],
        id: [
          { required: true, message: '请输入学号', trigger: 'blur' },
          {
            min: 1,
            max: 10,
            message: '用户名的长度在1~10个字符之间',
            trigger: 'blur'
          }
        ],
        department: [
          { required: true, message: '请输入学院', trigger: 'blur' },
          {
            min: 1,
            max: 10,
            message: '用户名的长度在1~10个字符之间',
            trigger: 'blur'
          }
        ],
        major: [
          { required: true, message: '请输入专业', trigger: 'blur' },
          {
            min: 1,
            max: 10,
            message: '用户名的长度在1~10个字符之间',
            trigger: 'blur'
          }
        ]
      },
      editFormRules: {
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 1,
            max: 20,
            message: '用户名的长度在3~20个字符之间',
            trigger: 'blur'
          }
        ],
        sex: [
          { required: true, message: '请输入性别', trigger: 'blur' },
          {
            min: 1,
            max: 5,
            message: '用户名的长度在1~5个字符之间',
            trigger: 'blur'
          }
        ],
        id: [
          { required: true, message: '请输入学号', trigger: 'blur' },
          {
            min: 1,
            max: 10,
            message: '用户名的长度在1~10个字符之间',
            trigger: 'blur'
          }
        ],
        department: [
          { required: true, message: '请输入学院', trigger: 'blur' },
          {
            min: 1,
            max: 10,
            message: '用户名的长度在1~10个字符之间',
            trigger: 'blur'
          }
        ],
        major: [
          { required: true, message: '请输入专业', trigger: 'blur' },
          {
            min: 1,
            max: 10,
            message: '用户名的长度在1~10个字符之间',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created () {
    this.getStudentList()
  },
  methods: {
    // 获取学生名单
    async getStudentList () {
      const { data: res } = await this.$http.get('student/queryAll')
      if (res === '') {
        return this.$message.error('获取学生列表失败！')
      }
      this.userlist = res
    },
    // 监听添加用户对话框的关闭事件
    addDialogClosed () {
      this.$refs.addFormRef.resetFields()
    },
    // 添加
    addStudent () {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return
        // 可以发起添加学生的网络请求
        const { data: res } = await this.$http.get(
          'student/insert?', {
            params: this.addForm
          })

        if (res === '') {
          this.$message.error('添加学生失败！')
        }

        this.$message.success('添加学生成功！')
        // 隐藏添加学生的对话框
        this.addDialogVisible = false
        // 重新获取学生列表数据
        this.getStudentList()
      })
    },
    // 删除
    async deleteStudent (id) {
      const { data: res } = await this.$http.get('student/delete?id=' + id)
      if (res === false) {
        this.$message.error('删除学生失败！')
      }

      this.$message.success('删除学生成功！')
      this.getStudentList()
    },
    // 查询
    async queryStudent () {
      const querypath = this.queryInfo.query
      const { data: ret } = await this.$http.get('student/queryById?id=' + querypath)
      this.userlist = [ret]
      // console.log(querypath)
    },
    // 修改学生对话框
    async showEditDialog (id) {
      const { data: ret } = await this.$http.get('student/queryById?id=' + id)
      console.log(ret)
      this.editForm = ret
      this.editDialogVisible = true
    },
    // 关闭对话框重置文本
    editDialogClosed () {
      this.$refs.editFormRef.resetFields()
    },
    // 修改
    editStudent () {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return
        // 可以发起添加学生的网络请求
        const { data: res } = await this.$http.get(
          'student/update?', {
            params: this.editForm
          })

        if (res === '') {
          this.$message.error('修改学生失败！')
        }

        this.$message.success('修改学生成功！')
        // 重新获取学生列表数据
        this.getStudentList()
        // 隐藏对话框
        this.editDialogVisible = false
      })
    }
  }
}
</script>

<style lang="less" scoped>
</style>
